package com.smec.remes.module.query.mantr.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewMantrBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewPackingBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewQuerymantrBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.query.QueryMantr;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/8.
 */

public class SmecQueryMantrAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private Context mcontext;
    private ArrayList<QueryMantr> arrayList=new ArrayList<>();
    private static int  NULLDATA = 0;
    private static int  DATA = 1;

    public void _clear(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public SmecQueryMantrAdapter(Context context) {
        this.mcontext=context;
    }

    public void setArrayList(ArrayList<QueryMantr> list){
        arrayList=list;
        notifyDataSetChanged();
    }


    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA){
            LayoutRecyclerviewQuerymantrBinding layoutRecyclerviewQuerymantrBinding=DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_querymantr,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewQuerymantrBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewQuerymantrBinding);
            return smecBaseRecyclerViewHolder;
        }else {
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == DATA) {
            LayoutRecyclerviewQuerymantrBinding layoutRecyclerviewQuerymantrBinding= (LayoutRecyclerviewQuerymantrBinding) holder.getBinding();
            layoutRecyclerviewQuerymantrBinding.setVariable(BR.queryMantr,arrayList.get(position));
        }
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        if(arrayList.size() == 0){
            return 1;
        }else{
            return arrayList.size();
        }

    }

    @Override
    public int getItemViewType(int position) {
        //在这里设置onCreateViewHolde方法内的viewType参数，然后根据这个参数来设置什么样的布局，是占位图还是显示数据的布局
        if(arrayList == null || arrayList.size() == 0){
            return NULLDATA;
        }else{
            return DATA;
        }
    }

}
